package com.rongke.web.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.commons.PageDto;
import com.rongke.model.Channel;
import com.rongke.model.WithdrawChannel;
import com.rongke.service.ChannelService;
import com.rongke.service.UserService;
import com.rongke.service.WithdrawChannelService;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @WithdrawChannelController
 * @渠道商提现记录Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/withdrawChannel")
@Transactional
@CrossOrigin
public class WithdrawChannelController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private WithdrawChannelService withdrawChannelService;
    @Autowired
    private UserService userService;
    @Autowired
    private ChannelService channelService;

    /**
     * @添加渠道商提现记录
     * @param withdrawChannel
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addWithdrawChannel(@RequestBody WithdrawChannel withdrawChannel){
        log.debug("添加渠道商提现记录");
        Channel channel = userService.getCurentChannel();
        withdrawChannel.setChannelId(channel.getId());
        withdrawChannel.setStatus(1);
        withdrawChannel.setGmtDatetime(new Date());
        withdrawChannel.setUptDatetime(new Date());
        withdrawChannelService.insert(withdrawChannel);
        BigDecimal bigDecimal = channel.getBalance().subtract(withdrawChannel.getMoney());
        channel.setBalance(bigDecimal);
        channelService.updateById(channel);
        return JsonResp.ok(withdrawChannel);
    }
    /**
     * @修改渠道商提现记录
     * @param withdrawChannel
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateWithdrawChannel(@RequestBody WithdrawChannel withdrawChannel){
        log.debug("修改渠道商提现记录");
        withdrawChannelService.updateById(withdrawChannel);
        return JsonResp.ok(withdrawChannel);
    }
    /**
     * @根据id查找渠道商提现记录
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectWithdrawChannel(Long id){
        log.debug("查找渠道商提现记录");
        WithdrawChannel withdrawChannel = withdrawChannelService.selectById(id);
        return JsonResp.ok(withdrawChannel);
    }

    /**
     * @渠道商分页查询自己提现记录
     * @param currentPage
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/findByPage", method = RequestMethod.GET)
    public JsonResp findByPage(Integer currentPage, Integer status){
        log.debug("渠道商分页查询自己提现记录");
        Channel channel = userService.getCurentChannel();
        EntityWrapper<WithdrawChannel> wrapper = new EntityWrapper<>();
        wrapper.eq("channel_id",channel.getId());
        if (status != -1) {
            wrapper.eq("status",status);
        }
        Page page = withdrawChannelService.selectPage(new Page(currentPage,10),wrapper);
        return JsonResp.ok(new PageDto(page.getCurrent(),10,page.getRecords(),page.getTotal()));
    }


    /**
     * @管理员分页查询渠道商提现记录
     * @param currentPage
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/adminFindByPage", method = RequestMethod.GET)
    @RequiresAuthentication
    public JsonResp adminFindByPage(Integer currentPage, Integer status){
        log.debug("管理员分页查询渠道商提现记录");
        EntityWrapper<WithdrawChannel> wrapper = new EntityWrapper<>();
        if (status != -1) {
            wrapper.eq("status",status);
        }
        Page page = withdrawChannelService.selectPage(new Page(currentPage,10),wrapper);
        return JsonResp.ok(new PageDto(page.getCurrent(),10,page.getRecords(),page.getTotal()));
    }


    /**
     * @更改渠道省借款记录状态
     * @param status
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/updateStatus", method = RequestMethod.GET)
    public JsonResp updateStatus(Integer status, Long id, String reason){
        log.debug("更改渠道省借款记录状态");
        WithdrawChannel withdrawChannel = withdrawChannelService.selectById(id);
        if (status == 2) {
            withdrawChannel.setReason(reason);
            BigDecimal money = withdrawChannel.getMoney();
            Channel channel = channelService.selectById(withdrawChannel.getChannelId());
            channel.setBalance(channel.getBalance().add(money));
            channelService.updateById(channel);
        }
        withdrawChannel.setStatus(status);
        withdrawChannelService.updateById(withdrawChannel);
        return JsonResp.ok();
    }




}
